package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.LetterDetail;

public class LetterDetailRowMapper implements RowMapper<LetterDetail> {

    @SuppressWarnings("unused")
    private int type;
    
    public static final int FOLLOW_BRIEF=1;

    public LetterDetailRowMapper() {
    }

    public LetterDetailRowMapper(int type) {
        this.type = type;
    }

    @Override
    public LetterDetail mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseHome(new LetterDetail(), rs);
    
    }

    public static LetterDetail parseHome(LetterDetail h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static LetterDetail parseHome(LetterDetail h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setLdid(rs.getLong(su+"ldid"));
        } catch (SQLException e) {
            h.setLdid(null);
        }
        try {
            h.setSuid(rs.getLong(su+"suid"));
        } catch (SQLException e) {
            h.setSuid(null);
        }
        try {
            h.setRuid(rs.getLong(su+"ruid"));
        } catch (SQLException e) {
            h.setRuid(null);
        }
        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            h.setContent(str);
        } catch (SQLException e) {
            h.setContent(null);
        }
        try {
            h.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            h.setState(null);
        }
        try {
            h.setSdel(rs.getShort(su+"sdel"));
        } catch (SQLException e) {
            h.setSdel(null);
        }
        try {
            h.setRdel(rs.getShort(su+"rdel"));
        } catch (SQLException e) {
            h.setRdel(null);
        }
        try {
            h.setImg(rs.getString(su+"img"));
        } catch (SQLException e) {
            h.setImg(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        try {
            h.setLid(rs.getLong(su+"lid"));
        } catch (SQLException e) {
            h.setLid(null);
        }
        try {
            h.setNickname(rs.getString(su+"nickname"));
        } catch (SQLException e) {
            h.setNickname(null);
        }
        try {
            h.setHeadimg(rs.getString(su+"headimg"));
        } catch (SQLException e) {
            h.setHeadimg(null);
        }
        try {
            h.setBelongid(rs.getInt(su+"belongid"));
        } catch (SQLException e) {
            h.setBelongid(0);
        }
        try {
            h.setIsreceive(rs.getInt(su+"isreceive"));
        } catch (SQLException e) {
            h.setIsreceive(1);
        }
        
        try {
            h.setIsFaild(rs.getInt(su+"isfaild"));
        } catch (SQLException e) {
            h.setIsFaild(0);
        }
        
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, LetterDetail h) {
        par.put("ldid",h.getLdid());
        par.put("suid",h.getSuid());
        par.put("ruid",h.getRuid());
        par.put("content",h.getContent());
        par.put("state",h.getState());
        par.put("sdel",h.getSdel());
        par.put("rdel",h.getRdel());
        par.put("img",h.getImg());
        par.put("cdate",h.getCdate());
        par.put("lid",h.getLid());
        par.put("nickname",h.getNickname());
        par.put("headimg",h.getHeadimg());
        par.put("belongid",h.getBelongid());
        par.put("isreceive",h.getIsreceive());
        par.put("isfaild",h.getIsFaild());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, LetterDetail h) {
        args.put("ldid",h.getLdid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final LetterDetail h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "ldid" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(LetterDetail h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into letter_detail(");
        int count = 0;
        if (null != h.getSuid()) {
            sql.append("suid,");
            count++;
        }
        if (null != h.getRuid()) {
            sql.append("ruid,");
            count++;
        }
        if (null != h.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != h.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != h.getSdel()) {
            sql.append("sdel,");
            count++;
        }
        if (null != h.getRdel()) {
            sql.append("rdel,");
            count++;
        }
        if (null != h.getImg()) {
            sql.append("img,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        if (null != h.getLid()) {
            sql.append("lid,");
            count++;
        }
        
        if (null != h.getLid()) {
            sql.append("belongid,");
            count++;
        }
        
        if (null != h.getLid()) {
            sql.append("isreceive,");
            count++;
        }
        
        if (null != h.getLid()) {
            sql.append("isfaild");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, LetterDetail h)
        throws SQLException {
        int count = 1;
        if (null != h.getSuid()) {
            ps.setLong(count++,h.getSuid());
        }
        if (null != h.getRuid()) {
            ps.setLong(count++,h.getRuid());
        }
        if (null != h.getContent()) {
            ps.setString(count++,h.getContent());
        }
        if (null != h.getState()) {
            ps.setShort(count++,h.getState());
        }
        if (null != h.getSdel()) {
            ps.setShort(count++,h.getSdel());
        }
        if (null != h.getRdel()) {
            ps.setShort(count++,h.getRdel());
        }
        if (null != h.getImg()) {
            ps.setString(count++,h.getImg());
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
        if (null != h.getLid()) {
            ps.setLong(count++,h.getLid());
        }
        
        if (null != h.getBelongid()) {
            ps.setLong(count++,h.getBelongid());
        }
        
        if (null != h.getIsreceive()) {
            ps.setLong(count++,h.getIsreceive());
        }
        if (null != h.getIsFaild()) {
            ps.setLong(count++,h.getIsFaild());
        }
    }
}
